package com.cash.app.utils

import android.content.Context
import android.content.res.Resources
import android.view.View
import com.cash.app.constant.AppConstant
import com.cash.app.constant.AppConstant.GENDER_FEMALE
import com.cash.app.constant.AppConstant.GENDER_FEMALE_STRING
import com.cash.app.constant.AppConstant.GENDER_MALE
import com.cash.app.constant.AppConstant.GENDER_MALE_STRING
import kotlin.math.roundToInt

fun View.hide() {
    visibility = View.GONE
}

fun View.inVisible() {
    visibility = View.INVISIBLE
}

fun View.visible() {
    visibility = View.VISIBLE
}

fun String.getGenderInt(): Int {
    return if (this == GENDER_MALE_STRING) GENDER_MALE
    else if (this == GENDER_FEMALE_STRING) GENDER_FEMALE
    else 1
}

fun String.addHttpPrefix(): String {
    return if (!this.contains(AppConstant.HTTP)) AppConstant.HTTP.plus(this) else this
}

fun String.isValidEmail(): Boolean {
    val emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
    return this.matches(emailPattern.toRegex())
}

// ================== dp → px ==================
// Get density correctly (get user dynamic scaling)
// System-level density (ignores user scaling settings)
fun Float.dpToPx(context: Context): Float = this * context.resources.displayMetrics.density

fun Int.dpToPx(context: Context): Int =
    (this * context.resources.displayMetrics.density).roundToInt()

fun Float.dpToPx(): Float = this * Resources.getSystem().displayMetrics.density

fun Int.dpToPx(): Int = (this * Resources.getSystem().displayMetrics.density).roundToInt()

// ================== px → dp ==================
fun Float.pxToDp(context: Context): Float = this / context.resources.displayMetrics.density

fun Int.pxToDp(context: Context): Float = this.toFloat() / context.resources.displayMetrics.density

fun Float.pxToDp(): Float = this / Resources.getSystem().displayMetrics.density

fun Int.pxToDp(): Float = this.toFloat() / Resources.getSystem().displayMetrics.density
